import React from 'react';
import {Button, Input, Form, Switch, Modal, message, InputNumber} from 'antd';
import _ from 'lodash';
const FormItem = Form.Item;
import {fetchArgs, FetchURL, createData, updateData, changePage, _validate, check_rules} from '../store/actions';

class FormAction extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: this.props.visible || false,
            title: this.props.title || '添加权限',
            targetData: this.props.targetData || {}
        };
        this.onOk = this.onOk.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.resetFields = this.resetFields.bind(this);
    };

    static contextTypes = {
        router: React.PropTypes.object
    };

    resetFields() {
        this.props.form.resetFields();
    };

    onOk(e) {
        const _this = this;
        _validate(e, _this, function (data) {
            data.code = data.code == null ? '' : data.code;
            data.status = (typeof data.status === 'boolean') ? (data.status ? 1 : 0) : (data.status === null ? 0 : data.status);
            let sendData = Object.assign({}, data, {});
            let _args = Object.assign({}, fetchArgs);
            _args.URL = FetchURL.ACTION;
            _args.IsAuth = true;

            if (!_this.props.id) {
                _args.SendData = sendData;
                _args.Succ = function (res) {
                    _this.onCancel();
                    message.success('新增权限成功', 1)
                    _this.setState({
                        visible: false,
                        id: null,
                        title: '',
                        targetData: null
                    }, function () {
                        _this.props.reFetch()
                    });
                };
                _this.props.dispatch(createData(_args));
            }
            else {
                sendData = Object.assign({}, data, {id: _this.props.id});
                _args.SendData = sendData;
                _args.Succ = function (res) {
                    _this.onCancel();
                    message.success('修改权限成功', 1)
                    _this.setState({
                        visible: false,
                        id: null,
                        title: '',
                        targetData: null
                    }, function () {
                        _this.props.reFetch()
                    });
                };
                _args.Fail = function () {
                    message.error('修改权限失败', 1);
                }
                _this.props.dispatch(updateData(_args));
            }
        });
    };

    onCancel() {
        this.props.onCancel();
        this.resetFields();
    };

    setInitialValue(data) {
        const {setFieldsValue} = this.props.form;
        setFieldsValue(data);
    };

    componentWillMount() {
        const _this = this;
        this.props.dispatch(check_rules(function (rules) {
            _this.setState({
                rules: rules.content
            })
        }));
    };

    componentWillReceiveProps(nextProp) {
        this.setState({
            visible: nextProp.visible,
            title: nextProp.title,
            targetData: nextProp.targetData
        });
    };

    componentDidMount() {
        this.setInitialValue(this.state.targetData);
    };

    render() {
        const {getFieldProps} = this.props.form;
        const formItemLayout = {
            labelCol: {span: 6},
            wrapperCol: {span: 16}
        };
        const name = getFieldProps('name', {
            rules: [
                {required: true, message: '请输入名称'}
            ],
            initialValue: this.state.targetData && this.state.targetData.name
        });
        const code = getFieldProps('code', {
            rules: [
                {required: false, message: '请输入编码'}
            ],
            initialValue: this.state.targetData && this.state.targetData.code
        });
        const desc0 = getFieldProps('desc0', {
            rules: [
                {required: false, message: '请输入描述'}
            ],
            initialValue: this.state.targetData && this.state.targetData.desc0
        });
        let _status = this.state.targetData ? (this.state.targetData.status == 1 ? true : false) : true;
        const status = getFieldProps('status', {
            rules: [
                {required: false, type: 'boolean', message: '请选择状态'}
            ],
            initialValue: _status
        });
        return (
            <Modal
                visible={this.state.visible}
                title={this.state.title}
                confirmLoading={this.state.confirmLoading}
                onOk={this.onOk}
                onCancel={this.onCancel}
            >
                <Form horizontal form={this.props.form}>
                    <FormItem {...formItemLayout} label="名称">
                        <Input {...name} placeholder="请输入名称"/>
                    </FormItem>
                    <FormItem {...formItemLayout} label="编码"><Input {...code} placeholder="请输入编码"/></FormItem>
                    <FormItem {...formItemLayout} label="描述"><Input type="textarea" {...desc0} placeholder="请输入描述"/></FormItem>
                    <FormItem {...formItemLayout} label="启用">
                        <Switch {...status} checked={status.value}/>
                    </FormItem>
                </Form>
            </Modal>
        );
    };
}
export default FormAction = Form.create()(FormAction);

